var vm = avalon.define({
	$id: "casherManage",
	toggle: false,
	pretoggle: false,
	token: localData.get('token'), //获取传过来的token值
	salelist: [],
	page: 1, //当前页
	pagesize: 10, //一页显示的几条数据
	total: 0, //总条数
	totalpage: [], //总页数
	pageisshow: false, //上一页下一页按钮是否显示
	cashier_id: '',
	datashow: false, //是否显示list
	//新增收银员页面跳转
	casherAdd: function() {
		top.window.location.href = "./casher-add.html";
	},
	receiptCode: function() { //收款码点击事件
		vm.toggle = true
	},
	closeClick: function() { //收款码关闭按钮点击事件
		vm.toggle = false; //关闭弹框
	},
	preCode: function() { //打开弹框
		vm.pretoggle = true
	},
	precloseClick: function() { //
		vm.pretoggle = false; //关闭弹框
	},
	deleteClick: function(id) { //删除收银员操作
		console.log(id);
		var r = confirm("确认删除吗？")
		if (r == true) {
			//删除收银员请求
			postData(api_cashierdelete, {
				token: vm.token,
				cashier_id: id
			}, function(res) {
				console.log(res);
				alert(res.msg);
			});

		} else {
			alert("您取消了删除!")
		}
	},
	modifyClick: function(id, departmentId, isAdmin) {
		console.log(id);
		console.log(isAdmin);
		if (isAdmin == '是') {
			alert("管理员修改请联系服务商")
		} else {
			top.window.location.href = "./casher-modify.html?cashierId=" + id + "&departmentId=" + departmentId
			// "./casher-modify.html?参数1=" + id + "&参数2="+ id

		}


	},
	//跳转到指定页面
	pageto: function(page) {
		vm.page = page;
		//收银员列表接口
		getData(api_cashier, {
			token: vm.token,
			page: vm.page,
			pagesize: vm.pagesize,
		}, function(res) {
			console.log(res);
			vm.bindDate(res);
			vm.datashow = true;
		});
	},
	//下一页
	pagenext: function() {
		if (vm.page < vm.totalpage.length) {
			vm.page = vm.page + 1;
		}
		//收银员列表接口
		getData(api_cashier, {
			token: vm.token,
			page: vm.page,
			pagesize: vm.pagesize,
		}, function(res) {
			console.log(res);
			vm.bindDate(res);
			vm.datashow = true;

		});
	},
	//上一页
	pagepre: function() {
		if (vm.page > 1) {
			vm.page = vm.page - 1;
		}
		//收银员列表接口
		getData(api_cashier, {
			token: vm.token,
			page: vm.page,
			pagesize: vm.pagesize,
		}, function(res) {
			console.log(res);
			vm.bindDate(res);
			vm.datashow = true;

		});
	},
	refresh: function() {
		//收银员列表接口
		getData(api_cashier, {
			token: vm.token,
			page: vm.page,
			pagesize: vm.pagesize,
		}, function(res) {
			console.log(res);
			alert("更新成功");
			if (res.status == 'SUCCESS') {
				vm.salelist = res.data[0].result.data;
				vm.total = res.data[0].result.total;
				vm.totalpage = [];
				if (vm.total > 10) {
					pageisshow = true;
				}
				for (var i = 1; i <= vm.total / vm.pagesize; i++) {
					vm.totalpage.push(i)
				}

			} else {
				alert(res.msg);
			}
		});
	},
	bindDate: function(res) {
		if (res.status == 'SUCCESS') {
			vm.salelist = res.data[0].result.data;
			console.log(vm.salelist);
			vm.total = res.data[0].result.total;
			vm.totalpage = [];
			
			if (vm.total > 10) {
				vm.pageisshow = true;
			} else {
				vm.pageisshow = false;
			}
			var totalpage = (parseInt(vm.total / vm.pagesize) < vm.total / vm.pagesize)?parseInt(vm.total / vm.pagesize)+1:parseInt(vm.total / vm.pagesize)
			
			if(totalpage < 10){
				for (var i = 1; i <= totalpage; i++) {
					vm.totalpage.push(i)
				}
			}else{
				console.log(totalpage);
				for (var i = 1; i <= totalpage; i++) {
					console.log(i);
					if(i==1||i==totalpage){
						if(vm.page+5<totalpage && i != 1){
							vm.totalpage.push("..." )
						}
						vm.totalpage.push(i)
						if(vm.page-5>1 && i != totalpage){
							vm.totalpage.push("...")
						}
						continue;
					}
					if(i>=vm.page-5 && i<= vm.page+5){
						vm.totalpage.push(i)
					}
				}
			}
		} else {
			alert(res.msg);
		}
	},

})
vm.$watch('onReady', function() {
	//收银员列表接口
	getData(api_cashier, {
		token: vm.token,
		page: vm.page,
		pagesize: vm.pagesize,
	}, function(res) {
		console.log(res);
		vm.bindDate(res);
		vm.datashow = true;

	});
})
